﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Protocol.section;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Protocol.building;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Weblications.Sango.Protocol.officer;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Protocol.party;
using Pixysoft.Weblications.Sango.Protocol.strategy;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.stage;
using Pixysoft.Weblications.Sango.Battle;

namespace Pixysoft.Weblications.Sango.Command.player
{
    partial class SangoPlayerHandler
    {
        public player_info player_info(double geox, double geoy)
        {
            string usercode = SangoManager.Authentication();

            player_info response = ReflectionManager.CreatePojo<player_info>();
            response.Succeed = false;

            Entities.Sys_Section sysSection = SysSectionHelper.trySearchSectionByGeolocation(geox, geoy);
            Entities.Cti_Section ctiSection = CitySectionHelper.searchSectionByCode(sysSection.Sectionserialcode);

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(usercode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(usercode);
            Entities.Usr_Activity activity = UsrActivityDao.getHistory(usercode);

            Entities.War_Map map = WarStageHelper.searchMap(property.Mapserialcode);

            Entities.Usr_Section section = UsrSectionDao.GetSection(usercode);
            List<Entities.Usr_Queue> queues = UsrQueueDao.getUsrQueues(profile.Usercode);
            List<Entities.Usr_Building> buildings = UsrBuildingDao.getUserBuildings(usercode);
            Entities.Usr_Building officerBuilding = UsrBuildingDao.trySearchUserBuilding(buildings, CtiBuildingTypecodeCollection.TRAIN);

            Entities.Usr_Partymember partymember = PartyDao.tryGetActiveMember(usercode);
            if (partymember != null)
            {
                Entities.Usr_Party party = PartyDao.tryGetActiveParty(partymember.Partyprimarycode);
                response.partymember = PartyFacade.ToPartyMember(profile, party, partymember);
            }

            List<Entities.Usr_Tactical> ormtacticals = TacticalDao.getUserTacticals(usercode);
            List<Entities.Usr_Strategy> strategies = StrategyDao.getStrategies(usercode);
            List<player_tactical> tacticals = new List<player_tactical>();
            foreach (Entities.Usr_Tactical ormtactical in ormtacticals)
            {
                Entities.Sys_Tactical sysTactical = SysTacticalHelper.search(ormtactical.Tacticaltypecode);
                Entities.Usr_Strategy strategy = StrategyDao.searchStrategy(strategies, sysTactical.Strategytypecode);
                tacticals.Add(UsrTacticalFacade.ToTactical(
                    strategy,
                    ormtactical,
                    TacticalDao.getUserTacticalPositions(ormtactical.Primarycode)));
            }
            response.tacticals = tacticals;

            Entities.Usr_Building houseBuilding = UsrBuildingDao.getUserBuilding(usercode, CtiBuildingTypecodeCollection.HOUSE);
            response.forcescoutprice = PlayerFormular.getForceScoutGoldprice(activity.Usedbattlecount + 1);
            response.scoutvalue = PlayerFormular.getScoutValue(profile.Userlevel, houseBuilding.Buildinglevel);
            response.trainprice = OfficerFormular.getUpgradeTimeModeCostSoulPrice(officerBuilding.Buildinglevel, UpgradeTimeTypecodeCollection.MODE_2);


            response.profile = UsrProfileFacade.ToProfile(profile);
            response.property = UsrProfileFacade.ToProperty(property);
            response.activity = UsrProfileFacade.ToActivity(profile, activity);

            response.geosection = UsrSectionFacade.ToSection(sysSection, ctiSection);
            response.section = UsrSectionFacade.ToSection(section);
            response.map = StageFacade.toMap(map);

            response.queues = UsrQueueFacade.ToQueues(queues);
            response.buildings = UsrBuildingFacade.ToBuildings(UsrBuildingDao.getUserBuildings(usercode), activity);
            response.userofficers = OfficerFacade.ToOfficers(OfficerDao.getUserOfficers(usercode));
            response.strategies = StrategyFacade.ToStrategies(strategies);

            response.usereqms = EquipmentFacade.ToEquipments(EquipmentDao.getUserEqms(usercode), EquipmentDao.getUserEqmExtras(usercode));


            RveStageTeam rveTeam = RveStageController.Instance.TryGetStage(usercode);
            if (rveTeam != null)
            {
                response.rve = rveTeam.Battledate;
                response.rvecode = rveTeam.Roomcode;
            }

            RvrSectionTeam rvrTeam = RvrSectionController.Instance.TryGetSectionTeam(usercode);
            if (rveTeam != null)
            {
                response.rvr = rvrTeam.Battledate;
                response.rvrcode = rvrTeam.TargetSection.Sectionserialcode;
            }

            response.Succeed = true;
            return response;
        }
    }

    public interface player_info : ISangoResponse
    {
        // reusable component

        player_section geosection { get;set;}
        player_section section { get;set;}
        stage_map map { get;set;}

        player_profile profile { get;set;}
        player_property property { get;set;}
        player_activity activity { get;set;}

        party_member partymember { get;set;}

        List<player_queue> queues { get;set;}
        List<building_building> buildings { get;set;}
        List<usr_equipment> usereqms { get;set;}
        List<player_officer> userofficers { get;set;}

        List<player_strategy> strategies { get;set;}
        List<player_tactical> tacticals { get;set;}

        /// <summary>
        /// 强征价格
        /// </summary>
        long forcescoutprice { get;set;}
        /// <summary>
        /// 可征银币数量
        /// </summary>
        long scoutvalue { get;set;}
        /// <summary>
        /// 征兵消耗粮草
        /// </summary>
        long recuitcost { get;set;}
        /// <summary>
        /// 征兵数量
        /// </summary>
        long recuitvalue { get;set;}
        /// <summary>
        /// 武将升级价格
        /// </summary>
        long trainprice { get;set;}

        /// <summary>
        /// 军团战
        /// </summary>
        long rve { get;set;}
        /// <summary>
        /// 战斗房间编码
        /// </summary>
        long rvecode { get;set;}
        /// <summary>
        /// 地区战
        /// </summary>
        long rvr { get;set;}
        /// <summary>
        /// 战斗地区编码
        /// </summary>
        long rvrcode { get;set;}

    }
}
